package com.epro.dx.activity.currency;

import java.util.ArrayList;

import com.epro.dx.R;
import com.epro.dx.util.DpPxUtils;
import com.epro.dx.util.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.weipu.common.facade.model.CurrencyModel;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.URLUtil;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author Tony
 * Currency页面列表数据适配器
 */
public class CurrencyListAdapter extends BaseAdapter {
	
	private Context mContext;
	private DisplayImageOptions options;
	private ArrayList<CurrencyModel> currencyModelList_canPay;//支持支付的币种
	private ArrayList<CurrencyModel> currencyModelList_showOnly;//仅支持展示的币种

	public CurrencyListAdapter(Context context,ArrayList<CurrencyModel> currencyModelList_canPay,ArrayList<CurrencyModel> currencyModelList_showOnly){
		this.mContext = context;
		this.currencyModelList_canPay = currencyModelList_canPay;
		this.currencyModelList_showOnly = currencyModelList_showOnly;
		options = ImageLoaderUtils.getImageLoaderOptions(R.drawable.item_default,R.drawable.item_default);
	}


	@Override
	public int getCount() {
		if(currencyModelList_canPay!=null && currencyModelList_showOnly!=null){
			int count_canPay = currencyModelList_canPay.size();
			int count_showOnly = currencyModelList_showOnly.size();
			return count_canPay>0?(count_canPay+count_showOnly+2):(count_showOnly>0)?(count_showOnly+1):0;
		}else{
			return 0;
		}
	}

	@Override
	public CurrencyModel getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder ;
		if(convertView != null&&convertView instanceof RelativeLayout){
			holder = (ViewHolder) convertView.getTag();
		}else{
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_currencylist, null);
			holder.flag = (ImageView) convertView.findViewById(R.id.item_currencylist_flag);
			holder.currencyText = (TextView) convertView.findViewById(R.id.item_currencylist_text);
			holder.isChecked = (ImageView) convertView.findViewById(R.id.item_currencylist_ischecked);
			convertView.setTag(holder);
		}
		//设置数据显示
		CurrencyModel model;
		if(currencyModelList_canPay.size()>0){//存在可用于支付的货币
			if(position==0){
				//返回可支付货币提示视图
				return listTitle(true);
			}
			if(position<=currencyModelList_canPay.size()){
				model = currencyModelList_canPay.get(position-1);
			}else{
				if(position == currencyModelList_canPay.size()+1){
					// 返回仅支持显示货币提示视图
					return listTitle(false);
				}
				model = currencyModelList_showOnly.get(position-currencyModelList_canPay.size()-2);
			}
		}else{//不存在可用于支付的货币
			if(position==0){
				//返回仅支持显示货币提示视图
				return listTitle(false);
			}
			model = currencyModelList_showOnly.get(position-1);
		}
//		if(position<currencyModelList_canPay.size()){
//			model = currencyModelList_canPay.get(position);
//		}else{
//			model = currencyModelList_showOnly.get(position-currencyModelList_canPay.size());
//		}
		holder.currencyText.setText(model.getCurrencyCode());
		if(model.isChecked()){
			holder.isChecked.setVisibility(View.VISIBLE);
		}else{
			holder.isChecked.setVisibility(View.INVISIBLE);
		}
		//加载图片
		String flagUrl = model.getFlagUrl();
		if(flagUrl!=null&&(URLUtil.isHttpsUrl(flagUrl)||URLUtil.isHttpUrl(flagUrl))){
			holder.flag.setVisibility(View.VISIBLE);
			ImageLoader.getInstance().displayImage(flagUrl, holder.flag, options);
		}else{
			holder.flag.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}
	
	/**
	 * 列表标题
	 * @param canPay 是否是可支付标题
	 * @return 对应标题视图
	 */
	public TextView listTitle(boolean canPay){
		TextView view = new TextView(mContext);
//		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, DpPxUtils.Dp2Px(mContext, 46));
//		view.setLayoutParams(layoutParams);
		view.setGravity(Gravity.CENTER_VERTICAL);
		view.setTextSize(14);
		view.setTextColor(Color.BLACK);
		view.setBackgroundColor(Color.TRANSPARENT);
		view.setPadding(DpPxUtils.Dp2Px(mContext, 8),DpPxUtils.Dp2Px(mContext, 10), DpPxUtils.Dp2Px(mContext, 8), DpPxUtils.Dp2Px(mContext, 10));
		if(canPay){
			view.setText(mContext.getString(R.string.currency_canPayTitle));
		}else{
			view.setText(mContext.getString(R.string.currency_showOnlyTitle));
		}
		return view;
	}
	
	class ViewHolder{
		ImageView flag;//国旗
		TextView currencyText;//币种
		ImageView isChecked;//是否勾选
	}

}
